package com.huang.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 自定义配置类
 * @Configuration修饰的类表示该类是Spring配置类
 * @author yili_gi
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

    /**
     * 添加视图解析器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //实现WebMvcConfigurer接口可以实现拓展WebMvcConfigurer的配置
        //重写addViewControllers()方法，添加一个视图控制器
        //添加视图控制，设置url-pattern，指向视图index。（前缀和后缀在Thymeleaf视图解析器中默认设置）
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main").setViewName("dashboard");
    }

    /**
     * 注册自定义视图解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    /**
     * 自定义拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
         registry.addInterceptor(new LoginHandlerInterceptor())
                 .addPathPatterns("/*")
                 .excludePathPatterns("/","/index.html","/login","/css/*","/js/*","/img/*");
    }
}
